package com.example.smartcity.ui.person.Adapter

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.example.smartcity.R
import com.example.smartcity.SmartCityApplication.Companion.context
import com.example.smartcity.logic.utils.Toast.Companion.showToast
import com.example.smartcity.ui.person.login.*
import com.example.smartcity.ui.person.model.ListItemRecyclerViewEntity
import kotlin.math.acos

/**
 *
 *  ANDROID STUDIO 2022
 *  @emil 2506789532@qq.com
 *  PersonListItemRecyclerViewAdapter --- @ikun 2023/2/7 13:26
 **/
class PersonListItemRecyclerViewAdapter(

    val data: List<ListItemRecyclerViewEntity>

    ) : RecyclerView.Adapter<PersonListItemRecyclerViewAdapter.ViewHolder>(){


    inner class ViewHolder(v: View) : RecyclerView.ViewHolder(v){
        // 获取控件
        val Image = v.findViewById<ImageView>(R.id.person_item_ImageView)
        val TvTitle = v.findViewById<TextView>(R.id.person_item_TvTitle)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val root = LayoutInflater.from(parent.context).inflate(R.layout.recyclerview_item_person, parent, false)
        val view = ViewHolder(root)

        view.itemView.setOnClickListener {

            if(view.TvTitle.text == "个人信息"){
                parent.context.getSharedPreferences("token", Context.MODE_PRIVATE).apply {
                    /**
                     *  判断登录状态
                     *  为 true 侧进入个人信息界面
                     *  为 false 侧进入登录界面并提示
                     */
                    if (this.getBoolean("user", false)) parent.context.startActivity(Intent(parent.context, infoActivity::class.java))
                    else apply {
                        parent.context.startActivity(Intent(parent.context, LoginActivity::class.java))
                        "您还没有登录! 请先登录。".showToast()
                    }
                }
            }

            if(view.TvTitle.text == "订单列表"){
                parent.context.getSharedPreferences("token", Context.MODE_PRIVATE).apply {
                    /**
                     *  判断登录状态
                     *  为 true 侧进入个人信息界面
                     *  为 false 侧进入登录界面并提示
                     */
                    if (this.getBoolean("user", false)) parent.context.startActivity(Intent(parent.context, OrderFormActivity::class.java))
                    else apply {
                        parent.context.startActivity(Intent(parent.context, LoginActivity::class.java))
                        "您还没有登录! 请先登录。".showToast()
                    }
                }

            }

            if(view.TvTitle.text == "修改密码"){
                parent.context.getSharedPreferences("token", Context.MODE_PRIVATE).apply {
                    /**
                     *  判断登录状态
                     *  为 true 侧进入个人信息界面
                     *  为 false 侧进入登录界面并提示
                     */
                    if (this.getBoolean("user", false)) parent.context.startActivity(Intent(parent.context, LoginPwdActivity::class.java))
                    else apply {
                        parent.context.startActivity(Intent(parent.context, LoginActivity::class.java))
                        "您还没有登录! 请先登录。".showToast()
                    }
                }

            }

            if(view.TvTitle.text == "意见反馈"){
                parent.context.getSharedPreferences("token", Context.MODE_PRIVATE).apply {
                    /**
                     *  判断登录状态
                     *  为 true 侧进入个人信息界面
                     *  为 false 侧进入登录界面并提示
                     */
                    if (this.getBoolean("user", false)) parent.context.startActivity(Intent(parent.context, FeedBackActivity::class.java))
                    else apply {
                        parent.context.startActivity(Intent(parent.context, LoginActivity::class.java))
                        "您还没有登录! 请先登录。".showToast()
                    }
                }

            }
        }

        return view
    }

    override fun getItemCount() = data.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val ld = data[position]
        // 设置数据
        holder.Image.setImageResource(ld.imageView)
        holder.TvTitle.text = ld.title
    }
}